---
title: "MRSL D2 Knights 2023"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
<!--------------------------------------------------------------------------------------
SET UP: PACKAGES, FUCNTIONS, DATA LAOD
------------------------------------------------------------------------------------>
```{r}
knitr::opts_chunk$set(
echo = F, warning = F, message = F
)
```
```{r packages }
library(ggthemes, quietly = TRUE)
library(htmltools, quietly = TRUE)
library(tidyverse)
library(plotly)
library(flexdashboard)
library(ggsoccer)
library(googleAuthR)
library(googlesheets4)
library(kableExtra)
```
```{r laod goals and assists data}
scores <-
read_sheet("https://docs.google.com/spreadsheets/d/1xy6CVRLfpO2tsVxcl5ZlwOGvqqlTipfcC4T4YBgM8ZY/edit#gid=0",
sheet = "Knights")
scores$Group <-
with(scores,
case_when(
`Player Position` %in% c("LW", "RW", "ST") ~ "Attackers",
`Player Position` %in% c("CM") ~ "Midfielders",
T ~ "Defenders"
))
scores$Group <- factor(scores$Group, levels = c("Defenders", "Midfielders", "Attackers"))
```
```{R load schedule and resutls data}
schedule <-
read_sheet("https://docs.google.com/spreadsheets/d/1xy6CVRLfpO2tsVxcl5ZlwOGvqqlTipfcC4T4YBgM8ZY/edit#gid=0",
sheet = "Schedule")
schedule <-
schedule %>%
filter(Division == "D2" & !is.na(`Final Score` )) %>%
mutate(
game = seq(from = 1, to = nrow(.), by = 1)
)
schedule$`Home Score` = with(schedule, substr(`Final Score`, 2, 2))
schedule$`Home Score` <- as.numeric(schedule$`Home Score`)
schedule$`Away Score` = with(schedule, substr(`Final Score`, 4, 4))
schedule$`Away Score` <- as.numeric(schedule$`Away Score`)
schedule$our_goals =
with(schedule,
case_when(
Home == "Knights" ~ `Home Score`,
T ~ `Away Score`
))
schedule$our_conceded =
with(schedule,
case_when(
Home == "Knights" ~ `Away Score`,
T ~ `Home Score`
))
schedule$opponent =
with(schedule,
case_when(
Home == "Knights" ~ Away,
T ~ Home
))
```
<!--------------------------------------------------------------------------------------
DASHBOARD BODY
------------------------------------------------------------------------------------>
Team Performance
===
Row
---
```{r}
schedule %>%
mutate(
goals_for = cumsum(our_goals),
goals_against = cumsum(our_conceded),
differential = goals_for - goals_against
) %>%
select(game, Date, goals_for, goals_against, differential, opponent) %>%
pivot_longer(
cols = c("goals_for", "goals_against"),
values_to = "Val",
names_to = "Type"
) %>%
mutate(
Type = case_when(
Type == "goals_for" ~ "Goals Scored",
T ~ "Goals Conceded"
)
) -> plot_df
plot_df %>%
plot_ly(
x = ~game,
y = ~Val,
color = ~Type,
type = "scatter",
mode = "lines+markers",
text =
with(. ,
paste(
"Date: ", Date,
"<br>Opponent: ", opponent,
"<br>Goals:", Val,
"<br>Type: ", Type,
"<br>Differential", differential
)),
hoverinfo = "text"
) %>%
layout(
yaxis = list(
range = c(0, max(cumsum(schedule$our_goals)) + 1)
)
)
```
Row
---
```{r}
scores %>%
filter(Action == "Assist") %>%
group_by(`Field Part`) %>%
summarize(n = n(),
p = n()/nrow(.)) ->
assists
assists <- assists %>% arrange(`Field Part`)
assists$y_lab = c(85,50,15)
assists$x_lab = c(75,75,75)
assists$label = with(assists, paste0(n, " (", round(p, 4)*100, "%)"
)
)
ggplot(data = assists,
aes(x = x_lab, y = y_lab, label = label)) +
annotate_pitch(colour = "#999e9b") +
theme_pitch(aspect_ratio = NULL) +
geom_point(color = "white") +
geom_text(size=7.5) +
geom_segment(
x = 30, xend = 89,
y = 67, yend = 67,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
arrow = arrow(length = unit(0.3, "inches")),
colour = "blue" # Also accepts "red", "blue' etc
)+
geom_segment(
x = 30, xend = 89,
y = 33, yend = 33,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
arrow = arrow(length = unit(0.3, "inches")),
colour = "blue" # Also accepts "red", "blue' etc
)
```
```{r}
scores %>%
filter(Action == "Assist") %>%
group_by(`Group`) %>%
summarize(n = n(),
p = n()/nrow(.)) ->
assists2
assists2 <- assists2 %>% arrange(`Group`)
assists2$y_lab = c(25,50,75)
assists2$x_lab = c(15,50,85)
assists2$label = with(assists2, paste0(n, " (", round(p, 4)*100, "%)"
)
)
ggplot(data = assists2,
aes(x = x_lab, y = y_lab, label = label)) +
annotate_pitch(colour = "#999e9b") +
theme_pitch(aspect_ratio = NULL) +
geom_point(color = "white") +
geom_text(size=7.5) +
geom_segment(
x = 67, xend = 67,
y = 10, yend = 90,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
colour = "red" # Also accepts "red", "blue' etc
)+
geom_segment(
x = 31, xend = 31,
y = 10, yend = 90,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
colour = "red" # Also accepts "red", "blue' etc
) +
geom_segment(
x = 15, xend = 25,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
) +
geom_segment(
x = 45, xend = 55,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
)+
geom_segment(
x = 75, xend = 85,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
)
```
```{r}
scores %>%
filter(Action == "Assist") %>%
group_by(`Pass Type`) %>%
summarise(n = n()) %>%
arrange(-n) %>%
rename(
Type = `Pass Type`
) -> pass_types
n_shot_types <- nrow(pass_types)
scores %>%
filter(Action == "Assist") %>%
group_by(`Position`) %>%
summarise(n = n()) %>%
arrange(-n) %>%
rename(
Type = `Position`
) -> pass_loc
n_shot_loc <- nrow(pass_loc)
shot_sum <- rbind(pass_types, pass_loc)
shot_sum %>%
kable(align = 'l') %>%
kable_styling(bootstrap_options = c("condesed", "striped")) %>%
pack_rows("Assist Types", 1, n_shot_types) %>%
pack_rows("Assist Locations", (n_shot_types+1), (n_shot_loc + n_shot_types))
```
Row
---
```{r}
scores %>%
filter(Action == "Goal") %>%
group_by(`Group`) %>%
summarize(n = n(),
p = n()/nrow(.)) ->
goals
goals <- goals %>% arrange(`Group`)
goals$y_lab = c(50,75)
goals$x_lab = c(50,85)
goals$label = with(goals, paste0(n, " (", round(p, 4)*100, "%)"
)
)
ggplot(data = goals,
aes(x = x_lab, y = y_lab, label = label)) +
annotate_pitch(colour = "#999e9b") +
theme_pitch(aspect_ratio = NULL) +
geom_point(color = "white") +
geom_text(size=7.5) +
geom_segment(
x = 67, xend = 67,
y = 10, yend = 90,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
colour = "red" # Also accepts "red", "blue' etc
)+
geom_segment(
x = 31, xend = 31,
y = 10, yend = 90,
lineend = "round", # See available arrow types in example above
linejoin = "round",
linetype = "dashed",
size = 2,
colour = "red" # Also accepts "red", "blue' etc
) +
geom_segment(
x = 15, xend = 25,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
) +
geom_segment(
x = 45, xend = 55,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
)+
geom_segment(
x = 75, xend = 85,
y = 85, yend = 85,
lineend = "round", # See available arrow types in example above
linejoin = "round",
size = 1,
arrow = arrow(length = unit(0.1, "inches")),
colour = "#7fb550" # Also accepts "red", "blue' etc
)
```
```{r}
scores %>%
filter(Action == "Goal") %>%
group_by(`Shot Type`) %>%
summarise(n = n()) %>%
arrange(-n) %>%
rename(
Type = `Shot Type`
) -> shot_types
n_shot_types <- nrow(shot_types)
scores %>%
filter(Action == "Goal") %>%
group_by(`Position`) %>%
summarise(n = n()) %>%
arrange(-n) %>%
rename(
Type = `Position`
) -> shot_loc
n_shot_loc <- nrow(shot_loc)
shot_sum <- rbind(shot_types, shot_loc)
shot_sum %>%
kable(align = 'l') %>%
kable_styling(bootstrap_options = c("condesed", "striped")) %>%
pack_rows("Shot Types", 1, n_shot_types) %>%
pack_rows("Shot Locations", (n_shot_types+1), (n_shot_loc + n_shot_types))
```
Individual Performance
===
Row
---
```{r}
scores %>%
filter(Action == "Assist") %>%
group_by(Player) %>%
summarise(Assists = n()) %>%
arrange(-Assists) -> ast
```
```{r}
scores %>%
filter(Action == "Goal") %>%
group_by(Player) %>%
summarise(Goals = n()) %>%
arrange(-Goals) -> gls
```
```{R}
merge(ast, gls, all = T, by = "Player") -> all_
all_$Assists <- ifelse(is.na(all_$Assists), 0, all_$Assists)
all_$Goals <- ifelse(is.na(all_$Goals), 0, all_$Goals)
all_$`Goals + Assists` <- all_$Assists + all_$Goals
all_ <- all_ %>% arrange(-`Goals + Assists`, -Goals, -Assists, Player)
all_ %>%
kable(align = 'c') %>%
kable_styling(bootstrap_options = c("striped", "condensed"))
```
Other
===
Row
---
```{r}
linkups <-
scores %>% filter(Action == "Goal") %>%
select(Date, Opponent, Score, Player, `Player Position`, `Play Type`) %>%
rename(Scorer = Player) %>%
left_join(
scores %>% filter(Action == "Assist") %>%
select(Date, Opponent, Score, Player) %>%
rename(Assister = Player),
by = c("Date", "Opponent", "Score")
)
linkups %>%
group_by(Assister) %>%
summarise(total_assist = n()) %>%
filter(!is.na(Assister)) %>%
left_join(
linkups %>%
group_by(
Assister, Scorer
) %>%
summarise(
person_assists = n()
),
by = "Assister"
) %>%
arrange(-total_assist , Assister) -> advanced_assist_tally
advanced_assist_tally
advanced_assist_tally %>%
rowwise() %>%
mutate(new_column = paste(sort(unique(c(Assister, Scorer))), collapse = " and ")) %>%
group_by(new_column) %>%
summarise(total = sum(person_assists)) %>%
arrange(-total)
```
```{R}
assistless <-
linkups %>% filter(is.na(Assister) & `Play Type` == "Open Play") %>%
group_by(
Scorer
) %>%
summarize(n = n()) %>%
arrange(-n)
```
```{r}
schedule %>%
group_by(
Venue
) %>%
summarise(
Games = n(),
For = sum(our_goals),
Against = sum(our_conceded)
)
```